<?php

namespace App\Admin\Forms\Service;

use App\Models\CashOrder;
use App\Models\Config as Model;
use App\Models\OrderCommission;
use App\Models\ServiceOrder;
use App\Models\ShopUser;
use App\Models\Worker;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class HandleCash extends Form implements LazyRenderable
{
    use LazyWidget;

    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
         $res = 0;
         if ($input){
             try {
                 $oid = $input['id'];
                 $status = $input['status'];
                 $order = CashOrder::whereId($oid)->first();
                 $order->status = $status;
                 $res = $order->save();
             }catch (ModelNotFoundException|Exception $e){
                 return $this->response()->error($e->getMessage());
             }
         }
         return $this->response()->success($res?'提交成功.':'暂无修改.')->refresh();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->display("out_trade_no");
        $this->display("user.nickname",'用户昵称');
        $this->display("banks.bank_name",'开户行名称');
        $this->display("banks.bank_num",'银行卡号');
        $this->display("banks.name",'开户姓名');
        $this->hidden("id");
        $this->display('amount','提现金额');
        $this->radio("status","审核")->options(['待审核','审核通过','审核拒绝'])->default(0);
    }

    /**
     * The data of the form.
     *
     * @return array
     */
    public function default()
    {
        return $this->getDefault();
    }

    protected function getDefault(){
        $oid = $this->payload['oid'];
        $order = CashOrder::whereId($oid)->with(['user'])->first();
        return $order->toArray();
    }
}
